package president;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Logs;
import controllers.PE;

public class ViewProperties extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model datePurchaseModel;
	private Model itemModel;
	private Model costModel;
	private Model sourceOfFundingModel;
	private Model propertyNoModel;
	private Model conditionModel;
	private Model dateCreatedModel;
	private Model uploadedByModel;
	
	public ViewProperties(int id) throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", PresidentHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		
		final int formid = id;
		PE pe = (PE)sqlMap.queryForObject("PE.getById", id);
	
		datePurchaseModel = new Model(pe.getDateOfPurchase());
		itemModel = new Model(pe.getItem());
		costModel = new Model(pe.getCost());
		sourceOfFundingModel = new Model(pe.getSourceOfFunding());
		propertyNoModel = new Model(pe.getPropertyNo());
		conditionModel = new Model(pe.getCondition());
		dateCreatedModel = new Model(pe.getDate_created());		
		uploadedByModel = new Model(pe.getUploader());
	
		Form form = new Form("viewForm");
		
		final String uploadby = ((Sessions)getSession()).getDesc();
		
		Date today = new Date();
		Timestamp timestamp = new Timestamp(today.getTime());
		final String ts = timestamp.toString();
		
		
		form.add(new Button("approve"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				PE pe = new PE();
				pe.setPe_id(formid);
				pe.setStatus("APPROVED");
				sqlMap.update("PE.updateStatus", pe);
				
				Logs log = new Logs();
				log.setLog_activity("Approved Properties&Equipment Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Properties page = new Properties();
				setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		form.add(new Button("reject"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				PE pe = new  PE();
				pe.setPe_id(formid);
				pe.setStatus("REJECTED");
				sqlMap.update("PE.updateStatus", pe);
				
				Logs log = new Logs();
				log.setLog_activity("Rejected Properties&Equipment Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Properties page = new Properties();
				setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		
		TextField date = new TextField("date", datePurchaseModel);
		form.add(date);
		
		TextField item = new TextField("item", itemModel);
		form.add(item);
		
		TextField cost = new TextField("cost", costModel);
		form.add(cost);
		
		TextField sourceOfFunding = new TextField("sourceOfFunding", sourceOfFundingModel);
		form.add(sourceOfFunding);
		
		TextField propertyNo = new TextField("propertyNo", propertyNoModel);
		form.add(propertyNo);
		
		TextField condition = new TextField("condition", conditionModel);
		form.add(condition);
		
		TextField dateCreated = new TextField("dateCreated", dateCreatedModel);
		form.add(dateCreated);
		
		TextField uploader = new TextField("uploader", uploadedByModel);
		form.add(uploader);
		
		add(form);
	}

}
